package org.hank.wanandroid.ui.fragment

import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.fragment_tab_article_project.*
import org.hank.commom.base.BaseFragment
import org.hank.wanandroid.R
import org.hank.wanandroid.bean.ProjectBean
import org.hank.wanandroid.listener.OnArticleRecyclerListener
import org.hank.wanandroid.ui.widget.ArticleRecycleWidget
import org.hank.wanandroid.viewmodel.ProjectTabViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/5/27 9:22
 *描述:
 */
class ProjectTabFragment(var position: Int = 0) :
    BaseFragment(R.layout.fragment_tab_article_project),
    OnArticleRecyclerListener {
    lateinit var projectPageFragment: ProjectPageFragment
    lateinit var projectBean: ProjectBean

    var articleRecycleWidget = ArticleRecycleWidget(this)
    var pageNum = 1 //项目页码从1开始
    var pageTotal = 0
    val pageTabeViewModel by getViewModel(ProjectTabViewModel::class.java) {
        projectDetailLivaData.observe(it) {
            pageTotal = it.pageCount
            if (pageNum < pageTotal) {
                pageNum += 1
            }
            if (it.datas != null && it.datas!!.size > 0)
                loadingTip.dismiss()
            else
                loadingTip.showEmpty()
            articleRecycleWidget.validateData(it.datas)
            dismissRefresh()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        projectPageFragment = parentFragment as ProjectPageFragment
        projectBean = projectPageFragment.viewModel.projectBeanLiveData.value!![position!!]
        var ft = childFragmentManager.beginTransaction()
        smartrefresh_tab_article_project.setOnRefreshListener {
            onReload()
        }
        smartrefresh_tab_article_project.setOnLoadMoreListener {
            if (pageNum < pageTotal)
                pageTabeViewModel.getProjectDetailsList(pageNum, projectBean.id)
            else {
                articleRecycleWidget.validateData(null)
                smartrefresh_tab_article_project.finishLoadMore()
            }
        }
        loadingTip.loading()
        onReload()
        ft.add(R.id.frame_tab_article_project, articleRecycleWidget)
        ft.commit()
    }

    override fun onReload() {
        pageNum = 1
        pageTabeViewModel.getProjectDetailsList(pageNum, projectBean.id)
        articleRecycleWidget.notifyReload()
    }

    fun dismissRefresh() {
        smartrefresh_tab_article_project.finishRefresh()
        smartrefresh_tab_article_project.finishLoadMore()
    }
}